package gui;

import gui.Main.GUI;
import gui.mapper.NodeListing;

import java.io.ObjectStreamException;
import java.io.Serializable;

import phase6.GlobalObjectId;
import phase6.LocalObjectId;

public class GUIFacade implements Serializable
{
	private static GUIFacade singleton = null;
	private static LocalObjectId localid = new LocalObjectId(phase6.GlobalLocalObjectIds.GUIFACADE_SINGLETON);

	private GUI main;

	protected GUIFacade()
	{
		// for use with the proxy object
		main = null;
	}

	private GUIFacade(GUI gui)
	{
		main = gui;
	}

	public static GUIFacade getSingleton(GUI gui)
	{
		if (singleton == null)
			singleton = new GUIFacade(gui);
		return singleton;
	}

	/**
	 * @pre |HyPeerWeb| = |GUI| - 1
	 * @post |HyPeerWeb| = |GUI|
	 * @param removedWebId
	 *            total number of nodes in HyPeerWeb
	 */
	public void nodeRemoved(int removedWebId)
	{
		NodeListing nl = main.getHyPeerWebDebugger().getMapper()
				.getNodeListing();
		nl.removeWebIdValue(removedWebId);
		
//		nl.clearList();
//		nl.initList();
		/*
		nl.decreaseListSize();
		assert (nl.listSize() == newHyPeerWebSize);
		*/
	}

	/**
	 * @pre |HyPeerWeb| = |GUI| + 1
	 * @post |HyPeerWeb| = |GUI|
	 * @param newHyPeerWebSize
	 *            total number of nodes in HyPeerWeb
	 */
	public void nodeAdded(int newNodeWebIdValue)
	{
		
		NodeListing nl = main.getHyPeerWebDebugger().getMapper()
				.getNodeListing();
		nl.addWebIdValueToList(newNodeWebIdValue);
		/*
		nl.increaseListSize();
		assert (nl.listSize() == newHyPeerWebSize);
		*/
	}
	
	/**
	 * @pre The HyPeerWeb has size 0
	 * @post The GUI has size 0
	 */
	public void webCleared()
	{
		main.getHyPeerWebDebugger().getMapper().getNodeListing().clearList();
	}

	public void printToTracePanel(Object msg)
	{
		main.getHyPeerWebDebugger().getTracePanel().println(msg);
	}
	
	public void printToErrorPanel(Object msg)
	{
		main.getHyPeerWebDebugger().getStatus().setContent(msg.toString());
	}

	public LocalObjectId getLocalId()
	{
		return localid;
	}
	
	private Object writeReplace() throws ObjectStreamException
	{
		return new GUIFacadeProxy(new GlobalObjectId(localid));
	}
}
